/** 表格数据渲染*/
window.TableColorLevelRender = function( crgb, minCol, num, range ){
    
    //获取td内的值array
    this.selfArray = [];
    //需要排列的td项
    this.originElements = [];

    this.tColor =  crgb || 'rgba(255,108,108,1)'; //颜色设置

    this.minColor = minCol || 0.029; //最小色阶

    this.slefNum = num || ''; //指定显示的td个数

    this.range = range || 3/10; //范围变量

    this.levelsElements = []; //着色的元素集合

}

window.TableColorLevelRender.prototype= {

    getSelfArray: function( obj ){

        var that = this;

        if( obj ){

            for( var i = 0, j; j = obj[i]; i++ ){

                if( parseInt(j.innerHTML) ) that.selfArray.push( parseInt(j.innerHTML) );
            }
        }
    },
    selfSort: function(arr){ //快速排序

        if(arr.length <= 1) return arr;

        var pivotIndex = Math.floor(arr.length / 2),

            pivot = arr.splice(pivotIndex, 1)[0],

            left = [],

            right = [];

        for(var i = 0, j; j = arr[i]; i++){

            if( j < pivot ) left.push(j);

            if( j >= pivot ) right.push(j);
        }

        return arguments.callee(right).concat( [pivot], arguments.callee(left));

    },

    setItem : function( obj )
    {
        this.originElements = obj || null;
    },

    setRender: function( arr){

        var that = this,

            oIndex = arr.length,

            i = 1,

            colorLevel = 0, //色阶比例

            colorLeft = '', //rgba的字符截取

            lastIndex = 0; //上一次色阶值

        if( arr.length <= 1 ) return;

        if( that.slefNum ){

            colorLevel = parseFloat( 1 / that.slefNum  );
        }else{

            colorLevel = parseFloat( 1 / (oIndex * that.range) ).toFixed(4);
        }

        colorLevel = that.minColor;

        that.slefNum = parseInt(arr.length * that.range);

        colorLeft = that.tColor.substr( 0, that.tColor.indexOf(')') - 1  );

        arr.map(function(ind, elem) {

            for( var t = 0, j; j = that.originElements[t]; t++ ){

                if( parseInt( j.innerHTML ) == ind && elem <= that.slefNum && parseFloat(lastIndex).toFixed(4) != parseFloat(i).toFixed(4) ){

                    j.style['backgroundColor'] = colorLeft + i + ')';

                    that.levelsElements.push( j );

                    lastIndex = i;
                }
            }

            if( i < colorLevel ){
                return;
            }else{

                i = i - colorLevel;
            }

        });

    },
    clearLevels: function(){ //清除色阶

        var that = this;
        if(that.levelsElements.length > 0){
            
            that.levelsElements.map(function(index, elem) {
                 
                 index.style['backgroundColor'] = '';
            })
        }

        that.levelsElements = [];

        that.selfArray = [];

    }
}